package com.net.xpay.web.controller.merchant.invoice;

import com.google.common.base.Preconditions;
import com.net.xpay.web.vo.ChargeRecordVo;
import com.net.xpay.web.vo.DeliveryAddressVo;
import com.net.xpay.web.vo.InvoiceTitleVo;
import com.net.xpay.web.vo.InvoiceVo;
import com.net.common.model.ResultData;
import com.net.common.model.ResultList;
import com.net.common.model.ResultPagedList;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.invoice.DeliveryAddress;
import com.net.xpay.common.domain.invoice.Invoice;
import com.net.xpay.common.domain.invoice.InvoiceTitle;
import com.net.xpay.common.manager.invoice.InvoiceManager;
import com.net.xpay.common.service.InvoiceService;
import com.net.xpay.core.domain.ChargeRecord;
import com.net.xpay.core.model.DeliveryAddressModel;
import com.net.xpay.core.model.InvoiceModel;
import com.net.xpay.core.model.InvoiceSearchModel;
import com.net.xpay.core.model.InvoiceTitleModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.web.secutiry.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 发票管理
 *
 86188
 */
@RestController
@Slf4j
@RequestMapping("/merchant/invoice")
@Api(tags = "商户/发票管理", description = "商户 发票管理 zc")
public class InvoiceController {

    @Autowired
    private InvoiceService invoiceService;

    @Autowired
    private InvoiceManager invoiceManager;


    /**
     * 查询开票记录
     */
    @ApiOperation("查询开票记录")
    @PostMapping("/search")
    public ResultPagedList<InvoiceVo> search(@ApiIgnore @RequestUser User merchant, @RequestBody InvoiceSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        searchModel.setUserId(merchant.getId());
        List<Invoice> records = invoiceManager.search(searchModel);
        List<InvoiceVo> vos = BeanCopierUtil.batchCopy(Invoice.class, InvoiceVo.class, records);
        long count = invoiceManager.searchCount(searchModel);
        return new ResultPagedList<>(vos, count, searchModel);
    }

    @ApiOperation("查询关联的消费记录")
    @PostMapping("/list-related-charge-record")
    public ResultList<ChargeRecordVo> listRelatedChargeRecord(@ApiIgnore @RequestUser User merchant, Long id) {
        List<ChargeRecord> list = invoiceService.listRelatedChargeRecord(merchant.getId(), id);
        List<ChargeRecordVo> vos = BeanCopierUtil.batchCopy(ChargeRecord.class, ChargeRecordVo.class, list);

        return new ResultList<>(vos);
    }

    /**
     * 查询发票抬头信息
     */
    @ApiOperation("查询发票抬头信息")
    @PostMapping("/get-invoice-title")
    public ResultList<InvoiceTitleVo> getInvoiceTitle(@ApiIgnore @RequestUser User merchant) {
        List<InvoiceTitle> invoiceTitle = invoiceService.listInvoiceTitle(merchant);
        List<InvoiceTitleVo> vos = BeanCopierUtil.batchCopy(InvoiceTitle.class, InvoiceTitleVo.class, invoiceTitle);
        return new ResultList<>(vos);
    }

    /**
     * 新增发票信息
     */
    @ApiOperation("新增发票抬头信息")
    @PostMapping("/save-invoice-title")
    public ResultData<Boolean> saveInvoiceTitle(@ApiIgnore @RequestUser User merchant, @RequestBody InvoiceTitleModel model) {
        Preconditions.checkArgument(model != null);
        model.setUserId(merchant.getId());
        invoiceService.saveInvoiceTitle(model);
        return new ResultData<>(true);
    }

    /**
     * 修改发票信息
     */
    @ApiOperation("修改发票抬头信息")
    @PostMapping("/update-invoice-title")
    public ResultData<Boolean> updateInvoiceTitle(@ApiIgnore @RequestUser User merchant, @RequestBody InvoiceTitleModel model) {
        Preconditions.checkArgument(model != null);
        model.setUserId(merchant.getId());
        invoiceService.updateInvoiceTitle(model);
        return new ResultData<>(true);
    }


    /**
     * 查询邮寄地址信息
     */
    @ApiOperation("查询邮寄地址信息")
    @PostMapping("/get-delivery-address")
    public ResultList<DeliveryAddressVo> getDeliveryAddress(@ApiIgnore @RequestUser User merchant) {

        List<DeliveryAddress> deliveryAddress = invoiceService.getDeliveryAddress(merchant);
        List<DeliveryAddressVo> vos = BeanCopierUtil.batchCopy(DeliveryAddress.class, DeliveryAddressVo.class, deliveryAddress);
        return new ResultList<>(vos);
    }

    /**
     * 新增邮寄地址
     */
    @ApiOperation("新增邮寄地址")
    @PostMapping("/save-delivery-address")
    public ResultData<Boolean> saveDeliveryAddress(@ApiIgnore @RequestUser User merchant, @RequestBody DeliveryAddressModel model) {
        Preconditions.checkArgument(model != null);
        model.setUserId(merchant.getId());
        invoiceService.saveDeliveryAddress(model);
        return new ResultData<>(true);
    }

    /**
     * 修改邮寄地址
     */
    @ApiOperation("修改邮寄地址")
    @PostMapping("/update-delivery-address")
    public ResultData<Boolean> updateDeliveryAddress(@ApiIgnore @RequestUser User merchant, @RequestBody DeliveryAddressModel model) {
        Preconditions.checkArgument(model != null);
        model.setUserId(merchant.getId());
        invoiceService.updateDeliveryAddress(model);
        return new ResultData<>(true);
    }

    /**
     * 删除邮寄地址
     */
    @ApiOperation("删除邮寄地址")
    @PostMapping("/delete-delivery-address")
    public ResultData<Boolean> deleteDeliveryAddress(@ApiIgnore @RequestUser User merchant, Long id) {

        invoiceService.deleteDeliveryAddress(merchant.getId(), id);

        return new ResultData<>(true);
    }

    /**
     * 设为默认地址
     */
    @ApiOperation("设为默认地址")
    @PostMapping("/is-default-delivery-address")
    public ResultData<Boolean> isDefaultDeliveryAddress(@ApiIgnore @RequestUser User merchant, Long id) {
        invoiceService.setAsDefaultDeliveryAddress(merchant.getId(), id);
        return new ResultData<>(true);
    }

    /**
     * 创建发票
     */
    @ApiOperation("创建发票")
    @PostMapping("/add-invoice")
    public ResultData<Boolean> addInvoice(@ApiIgnore @RequestUser User merchant, @RequestBody InvoiceModel model) {
        invoiceService.addInvoice(merchant.getId(), model);
        return new ResultData<>(true);
    }


}
